package com.mobdev.notepad.shared.ui;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.mobdev.notepad.shared.DBConstants;
import com.mobdev.notepad.shared.NotepadStrings;

/**
 * An activity that will edit the title of a note. Displays a floating
 * window with a text field.
 */
public abstract class AbstractTitleEditor extends Activity implements View.OnClickListener
{
    /**
     * An array of the columns we are interested in.
     */
    private static final String[] PROJECTION = new String[] {
    	DBConstants._ID, // 0
        DBConstants.TITLE, // 1
    };
    /** Index of the title column */
    private static final int COLUMN_INDEX_TITLE = 1;

    /**
     * Cursor which will provide access to the note whose title we are editing.
     */
    protected Cursor mCursor;

    /**
     * The EditText field from our UI. Keep track of this so we can extract the
     * text when we are finished.
     */
    protected EditText mText;

    /**
     * The content URI to the note that's being edited.
     */
    private Uri mUri;

    // This is our state data that is stored when freezing.
    private static final String ORIGINAL_TITLE = "origTitle";

    private String mOriginalTitle;


    protected abstract void init();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();

        setTitle(NotepadStrings.title_edit_title);

        // Get the uri of the note whose title we want to edit
        mUri = getIntent().getData();

        // Get a cursor to access the note
        mCursor = managedQuery(mUri, PROJECTION, null, null, null);


        // If an instance of this activity had previously stopped, we can
        // get the original text it started with.
        if (savedInstanceState != null) {
        	mOriginalTitle = savedInstanceState.getString(ORIGINAL_TITLE);
        }

    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // Save away the original text, so we still have it if the activity
        // needs to be killed while paused.
    	outState.putString(ORIGINAL_TITLE, mOriginalTitle);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Initialize the text with the title column from the cursor
        if (mCursor != null) {
            mCursor.moveToFirst();

            String title = mCursor.getString(COLUMN_INDEX_TITLE);
            mText.setText(title);

            if(mOriginalTitle == null)
            {
                mOriginalTitle = title;
            }
        }
    }


    public void onClick(View v) {
		 if (mCursor != null) {

	         String currentTitle = mText.getText().toString();
	         if(currentTitle.trim().length() == 0) {
	        	 Toast.makeText(mText.getContext(), "Title cannot be empty.", Toast.LENGTH_SHORT).show();
	        	 return; // don't exit title editor
	         }

	         if(!currentTitle.equals(mOriginalTitle))
	         {
	         	// Write the title back to the note
	            ContentValues values = new ContentValues();
	            values.put(DBConstants.TITLE, currentTitle);
	            values.put(DBConstants.MODIFIED_ON, System.currentTimeMillis()/1000);
	            getContentResolver().update(mUri, values, null, null);
	         }
	     }


		 finish();
    }
}
